package com.base.cn.platform.os.service.agent.user;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.agent.user.AgentUserClientDao;
import com.base.cn.platform.os.entity.agent.user.AgentUser;
import com.base.cn.platform.os.entity.agent.user.AgentUserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 推广员客户记录Biz
 *
 * @author s.li
 * @date 2018/11/20
 */
@Service
public class AgentUserClientBiz extends BaseBiz<AgentUserClient, AgentUserClientDao> {

    @Autowired
    private AgentUserBiz agentUserBiz;

    /**
     * 保存推广员客户记录
     * @param agentUserClient 记录对象
     */
    @Transactional(rollbackFor = IllegalArgumentException.class)
    public void saveAgentUserClient(AgentUserClient agentUserClient){
        AgentUserClient _userClient = this.findOne(" userId ="+agentUserClient.getUserId(),null);
        //如果不是别人的客户，就可以添加
        if(ObjectUtils.isEmpty(_userClient)){
            this.save(agentUserClient);
            //修改推广员的客户数量
            AgentUser agentUser = agentUserBiz.findById(agentUserClient.getAgentId());
            agentUser.setCustomerNum(agentUser.getCustomerNum().intValue() + 1);
            agentUserBiz.updateById(agentUser,null);
        }
    }
}
